<?php

function list_jabatan_specification(){
	
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'No Org '=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		'Departemen'=>array('style'=>'border-bottom:2px solid;width:30%'),   	
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);
	$facebox=false;
	
	$com = isset($_GET['com']) ? $_GET['com']: '';
	$task = isset($_GET['task']) ? $_GET['task']: '';
	$field = isset($_GET['field']) ? $_GET['field']: '';
	$key = isset($_GET['key']) ? $_GET['key']: '';
	if(isset($_GET['key'])){
		$query = "
			SELECT a.* 
			FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id 
			OR a.nomor_posisi ='{$_GET['key']}' 
			AND b.active='Y' 
			ORDER BY a.urut
		";
	}else{
		$query = "SELECT a.* 
			FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id 
			WHERE b.active='Y' 
			ORDER BY a.urut
		" ;
	}
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =20;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	$last_page_text = null;
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,
				$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page);  
	
	$i = ($current_page  - 1 ) * 20; 
	$row = array();

	while($ey = my_fetch_array($result) ){
		$i++;
		$specificationproperty = array(
			'href'=>'index.php?com=jabatan_specification&task=list_specification&jabatan_id='.$ey['jabatan_id'],
			'title'=>'Job Specification'
		);
		$specification_button = button_icon( 'b_dbstatistics.png' , $specificationproperty  );
		
		$kompetensiproperty = array(
			'href'=>'index.php?com=jabatan_specification&task=list_kompetensi&jabatan_id='.$ey['jabatan_id'] ,
			'title'=>'Kompetensi'
		);
		$kompetensi_button = button_icon( 'b_newdb.png' , $kompetensiproperty  );

		$physicalproperty = array(
			'href'=>'index.php?com=jabatan_specification&task=list_physical&jabatan_id='.$ey['jabatan_id'] ,
			'title'=>'Physical Ability'
		);
		$physical_button = button_icon( 'b_props.png' , $physicalproperty  );
		
		$departemen = my_get_data_by_id('departemen' ,'departemen_id', $ey['departemen_id']);
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nomor'=>$ey['nomor_posisi'],
			'jabatan'=>  $ey['nama_jabatan'] ,
			'departemen'=> $departemen['nama_departemen'] , 
			'operasi'=> position_text_align(  $specification_button.' '. $kompetensi_button.' '.$physical_button , 'right'),  
		);
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list(array() , $search);
	return $printmenu.table_builder($header , $datas ,  6 , false  , $paging );
}
/*
===============================
	SPECIFICATION
===============================
*/	
function list_specification($jabatan_id){
	$jabatan = loaddata_jabatan($jabatan_id) ;
	$view  = form_header( "detail jabatan" , "cp"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value=" BACK " class="main_button" onclick="javascript:location.href=\'index.php?com=jabatan_specification\'"/>
	</div></td></tr>'   ; 	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nomor_posisi'] .'</span>'  , "<b>Nomor Posisi</b>"    );  	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nama_jabatan'].'</span>'   , "<b>Jabatan</b>"    );  
	$view .= form_field_display( '<span class="label_form">'.$jabatan['departemen'] .'</span>'  , "<b>Departemen</b>"    ); 
	//$view .= form_field_display( '<span class="label_form">'.$jabatan['organisasi_direktorat'] .'</span>'  , "<b>Direktorat</b>"    ); 
	$view .= form_footer( ); 
	
	$aaaa = level_pendidikan_tab($jabatan_id);
	$bbbb =golongan_jabatan_tab($jabatan_id);
	  
	$tab_options = array(
		'Pendidikan/Masa Kerja'=>'tab0',
		'Golongan Jabatan'=>'tab1'
	);
	$contents = array(
		'tab0'=>$aaaa,
		'tab1'=>$bbbb  
	);
	$view .= tab_page($tab_options , $contents );
	
	unset($_SESSION['tab_page']);
	return $view  ;
}

function level_pendidikan_tab($jabatan_id){
//	$_SESSION['tab_page'] = '#tab0';
	my_set_code_js('
		function confirmDeletePendidikan(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=level_pendidikan_delete&id=\'+id;
			}
			return false;
		}		
	');	
	$header  = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Tingkat Pendidikan'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		'Masa Kerja'=>array('style'=>'border-bottom:2px solid;width:45%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'),   
	);
	$query = "SELECT * FROM jabatan_pendidikan_link a
		INNER JOIN pendidikan b ON a.level_pendidikan_id = b.pendidikan_id
		WHERE a.jabatan_id ={$jabatan_id}";
	$result = my_query($query);
	$i = 0;
	$row = array();
	while($r = my_fetch_array($result) ){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=jabatan_specification
				&task=level_pendidikan
				&jabatan_id='.$jabatan_id.'
				&id='.$r['jabatan_pendidikan_link_id'],			
			'title'=>'Edit',
			'rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$deleteproperty = array(
			'href'=>'javascript:void();', 
			'title'=>'Edit',
			'onclick'=>'javascript:confirmDeletePendidikan( '.$r['jabatan_pendidikan_link_id'].')'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'#'=> $i,  
			'level'=> $r['label'].' - '.$r['major'] ,  
			'masa'=>  $r['masa_kerja'].' tahun',  
			'edit'=>position_text_align(  $edit_button .' '.$delete_button,'right')
		); 
	}
	if($i < 5){
		for($ycc=$i; $ycc<=4; $ycc++){
			$row[] = array(
				'#'=> '&nbsp;',  
				'lev'=>  '&nbsp;', 
				'nama'=>  '&nbsp;', 
				'sas'=>  '&nbsp;',   
			); 
		}
	}
	$datascc = table_rows_plain($row ); 
	$eeee  = table_builder_plain($header  , $datascc ,  5,false ); 
	return $eeee. '<div style="text-align:right;width:100%;font:12px verdana;">'
		.admin_link('<a href="index.php?com=jabatan_specification
		&task=level_pendidikan
		&jabatan_id='.$jabatan_id.'
		&id=0"
		 rel="facebox">[Tambah data]</a>' )
		.'</div>'; 
}

function form_level_pendidikan_create($jabatan_id , $id){
	$view = form_header( "jabatan_specification" , "jabatan_specification"  );
 	$fields = my_get_data_by_id('jabatan_pendidikan_link','jabatan_pendidikan_link_id',$id);
	$jabatan = my_get_data_by_id('jabatan','jabatan_id',$jabatan_id );
	$view .= form_field_display( ucfirst($jabatan['nomor_posisi']) , 'Nomor Posisi' ); 
	$view .= form_field_display( ucfirst($jabatan['nama_jabatan']) , 'Jabatan' );

	$levelpendidikans = array();
	$query_golongan = "SELECT * FROM pendidikan order by label, major";
	$re = my_query($query_golongan);
	while($row = my_fetch_array($re) ){
		$levelpendidikans[$row['pendidikan_id']] =$row['label']."-".$row['major'];
	}
	$levelpendidikan = array(
		'name'=>'level_pendidikan_id',
		'id'=>'level_pendidikan_id',
		'value'=>isset($_POST['level_pendidikan_id']) ? $_POST['level_pendidikan_id'] : $fields['level_pendidikan_id'] 
	);
	$form_field_level = form_dropdown($levelpendidikan , $levelpendidikans);
	$view .= form_field_display( $form_field_level   , "Tingkat Pendidikan");

	$masakerja = array(
		'name'=>'masa_kerja',
		'id'=>'masa_kerja',
		'value'=>isset($_POST['masa_kerja']) ? $_POST['masa_kerja'] : $fields['masa_kerja'],
		'type'=>'textfield',
		'size'=>'10',
	);
	$form_masa_kerja = form_dynamic($masakerja);
	$view .= form_field_display( $form_masa_kerja."&nbsp;".'tahun' , "Masa Kerja" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_level_pendidikan_submit($jabatan_id, $id= 0){
	$datas = array(
		'jabatan_id'=>my_type_data_int($jabatan_id),
		'masa_kerja'=>my_type_data_int($_POST['masa_kerja']),
		'level_pendidikan_id'=>my_type_data_int($_POST['level_pendidikan_id'])
	);
	if($id > 0){
		return my_update_record('jabatan_pendidikan_link','jabatan_pendidikan_link_id',$id, $datas);	
	}
	return my_insert_record('jabatan_pendidikan_link' , $datas);
}

function form_level_pendidikan_validate($jabatan_id , $id){
	$errsubmit = false;
	$err = array();
 
	$code =  $_POST["level_pendidikan_id"]; 
	if( $code == 0){
		$errsubmit = true;
		$err[] = "Tingkat Pendidikan belum di isi";
	}

	$query="SELECT jabatan_pendidikan_link_id FROM jabatan_pendidikan_link 
		WHERE level_pendidikan_id=".$code.
		" AND jabatan_id=".$jabatan_id; 
		" AND jabatan_pendidikan_link_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Tingkat Pendidikan sudah digunakan";	
	}
 
	$code =  trim($_POST["masa_kerja"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Masa kerja belum di isi";
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_level_pendidikan($id){
 
	$query = "DELETE FROM jabatan_pendidikan_link WHERE jabatan_pendidikan_link_id ={$id} ";
	 
	return my_query($query);
}

function golongan_jabatan_tab($jabatan_id){
//$_SESSION['tab_page'] = '#tab1';
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=golongan_jabatan_delete&id=\'+id;
			}
			return false;
		}		
	');	
	$header  = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Golongan Jabatan'=>array('style'=>'border-bottom:2px solid;width:85%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'),   
	);
	$query = "SELECT * FROM jabatan_golongan_link a
		INNER JOIN golongan_jabatan b ON a.golongan_jabatan_id = b.golongan_jabatan_id
		WHERE a.jabatan_id ={$jabatan_id}";
	$result = my_query($query);
	$i = 0;
	$row = array();
	while($r = my_fetch_array($result) ){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=jabatan_specification
				&task=golongan_jabatan&jabatan_id='.$jabatan_id.'
				&id='.$r['jabatan_golongan_link_id'],			
			'title'=>'Edit',
			'rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$deleteproperty = array(
			'href'=>'javascript:void();', 
			'title'=>'Edit',
			'onclick'=>'javascript:confirmDelete( '.$r['jabatan_golongan_link_id'].')'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'#'=> $i,  
			'level'=> $r['kode_golongan_jabatan'] ,  
			'edit'=>position_text_align(  $edit_button .' '.$delete_button,'right')
		); 
	}
	if($i < 5){
		for($ycc=$i; $ycc<=4; $ycc++){
			$row[] = array(
				'#'=> '&nbsp;',  
				'lev'=>  '&nbsp;',  
				'sass'=>  '&nbsp;',  
			); 
		}
	}
	$datascc = table_rows_plain($row ); 
	$eeee  = table_builder_plain($header  , $datascc ,  5,false ); 
	return $eeee. '<div style="text-align:right;width:100%;font:12px verdana;">'
		.admin_link('<a href="index.php?com=jabatan_specification
		&task=golongan_jabatan
		&jabatan_id='.$jabatan_id.'
		&id=0"
		 rel="facebox">[Tambah data]</a>' )
		.'</div>'; 
}

function form_golongan_jabatan_create($jabatan_id , $id){
	$view = form_header( "jabatan_specification" , "jabatan_specification"  );
 	$fields = my_get_data_by_id('jabatan_golongan_link','jabatan_golongan_link_id',$id);
	$jabatan = my_get_data_by_id('jabatan','jabatan_id',$jabatan_id );
	$view .= form_field_display( ucfirst($jabatan['nomor_posisi']) , 'Nomor Posisi' ); 
	$view .= form_field_display( ucfirst($jabatan['nama_jabatan']) , 'Jabatan' );

	$golongans = array();
	$query_golongan = "SELECT * FROM golongan_jabatan";
	$re = my_query($query_golongan);
	while($row = my_fetch_array($re) ){
		$golongans[$row['golongan_jabatan_id']] =$row['kode_golongan_jabatan'];
	}
	$golongan = array(
		'name'=>'golongan_jabatan_id',
		'id'=>'golongan_jabatan_id',
		'value'=>isset($_POST['golongan_jabatan_id']) ? $_POST['golongan_jabatan_id'] : $fields['golongan_jabatan_id'] 
	);
	$form_field_level = form_dropdown($golongan , $golongans);
	$view .= form_field_display( $form_field_level   , "Golongan Jabatan");

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_golongan_jabatan_submit($jabatan_id, $id= 0){
	$datas = array(
		'jabatan_id'=>my_type_data_int($jabatan_id),
		'golongan_jabatan_id'=>my_type_data_int($_POST['golongan_jabatan_id'])
	);
	if($id > 0){
		return my_update_record('jabatan_golongan_link','jabatan_golongan_link_id',$id, $datas);	
	}
	return my_insert_record('jabatan_golongan_link' , $datas);
}

function form_golongan_jabatan_validate($jabatan_id,$id){
	$errsubmit = false;
	$err = array();
	 
	$code =  (int) $_POST["golongan_jabatan_id"]; 
	if( $code == 0){
		$errsubmit = true;
		$err[] = "Golongan Jabatan belum di isi";
	}

	$query="SELECT jabatan_golongan_link_id FROM jabatan_golongan_link 
		WHERE golongan_jabatan_id=".$code.
		" AND jabatan_id=".$jabatan_id.
		" AND jabatan_golongan_link_id<>".$id ; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Golongan Jabatan sudah digunakan";	
	}
 
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_golongan_jabatan($id){
	$query = "DELETE FROM jabatan_golongan_link WHERE jabatan_golongan_link_id ={$id} ";
	return my_query($query);
}

/*
===============================
	KOMPETENSI
===============================
*/	
function list_kompetensi($jabatan_id){

	$jabatan = loaddata_jabatan($jabatan_id) ; 
	$view  = form_header( "detail jabatan" , "cp"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value=" BACK " class="main_button" onclick="javascript:location.href=\'index.php?com=jabatan_specification\'"/>
	</div></td></tr>'   ; 	
	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nomor_posisi'] .'</span>'  , "<b>Nomor Posisi</b>"    );  	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nama_jabatan'].'</span>'   , "<b>Jabatan</b>"    );  
	$view .= form_field_display( '<span class="label_form">'.$jabatan['departemen'] .'</span>'  , "<b>Departemen</b>"    );  
	$view .= form_footer( ); 
	
	$aaaa = kompetensi_tab($jabatan_id , 1);
	$bbbb =kompetensi_tab($jabatan_id , 2);
	$cccc = kompetensi_tab($jabatan_id , 3);
	  
	$tab_options = array(
		'Generik'=>'tab0',
		'Kepemimpinan'=>'tab1',
		'Teknis'=>'tab2'  
	);
	$contents = array(
		'tab0'=>$aaaa,
		'tab1'=>$bbbb, 
		'tab2'=>$cccc,  
	);
	$view .= tab_page($tab_options , $contents );
	
	unset($_SESSION['tab_page']);
	return $view.$tabcontents ;
	 
}

function kompetensi_tab($jabatan_id , $direktory_id){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=kompetensi_delete&id=\'+id;
			}
			return false;
		}		
	');	
	$header  = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Kode'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Kompetensi'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Level'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Indikator'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'),   
	);
	$query = "SELECT * FROM jabatan_kompetensi_link a
		INNER JOIN kompetensi b ON a.kompetensi_id = b.kompetensi_id
		INNER JOIN kompetensi_unit c ON b.kompetensi_unit_id = c.kompetensi_unit_id
		INNER JOIN  kompetensi_kelompok d ON d.kompetensi_kelompok_id = c.kompetensi_kelompok_id
		INNER JOIN kompetensi_jenis   e ON e.kompetensi_jenis_id = d.kompetensi_jenis_id
		INNER JOIN kompetensi_indikator f ON f.kompetensi_id = b.kompetensi_id AND 	f.tingkat_id = a.level
		WHERE a.jabatan_id ={$jabatan_id} AND e.kompetensi_jenis_id ={$direktory_id} ";
	$result = my_query($query);
	$i = 0;
	$row = array();
	while($r = my_fetch_array($result) ){
		$i++;
		
		$editproperty = array(
			'href'=>'index.php?com=jabatan_specification
			&task=edit_kompetensi
			&jabatan_id='.$jabatan_id.'
			&jenis_id='.$direktory_id.'
			&id='.$r['jabatan_kompetensi_link_id'], 
			'title'=>'Edit',
			'rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$deleteproperty = array(
			'href'=>'javascript:void();', 
			'title'=>'Edit',
			'onclick'=>'javascript:confirmDelete( '.$r['jabatan_kompetensi_link_id'].')'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'#'=> $i,  
			'kode'=>  ucfirst($r['kode_kompetensi']),  
			'nama'=>  ucfirst($r['nama_kompetensi']),  
			'level'=>    $r['level']   ,  
			'indikator'=>    $r['indikator']   ,  
			'edit'=>position_text_align(  $edit_button .' '.$delete_button,'right')
		); 
	}
	if($i < 5){
		for($ycc=$i; $ycc<=4; $ycc++){
			$row[] = array(
				'#'=> '&nbsp;',  
				'nama'=>  '&nbsp;', 
				'sas'=>  '&nbsp;',  
				'lev'=>  '&nbsp;', 
				'ind'=>  '&nbsp;', 
				'sass'=>  '&nbsp;',  
			); 
		}
	}
	$datascc = table_rows_plain($row ); 
	$eeee  = table_builder_plain($header  , $datascc ,  5,false ); 
	return $eeee. '<div style="text-align:right;width:100%;font:12px verdana;">'
		.admin_link('<a href="index.php?com=jabatan_specification
		&task=edit_kompetensi
		&jenis_id='.$direktory_id.'
		&jabatan_id='.$jabatan_id.'
		&id=0"
		 rel="facebox">[Tambah data]</a>' )
		.'</div>'; 
	
}

function form_kompetensi_create($jabatan_id , $id , $jenis_id){
	$view = form_header( "jabatan_kompetensi" , "jabatan_kompetensi"  );
 	$fields = my_get_data_by_id('jabatan_kompetensi_link','jabatan_kompetensi_link_id',$id);
	$jabatan = my_get_data_by_id('jabatan','jabatan_id',$jabatan_id );
	$view .= form_field_display( ucfirst($jabatan['nomor_posisi']) , 'Nomor Posisi' ); 
	$view .= form_field_display( ucfirst($jabatan['nama_jabatan']) , 'Jabatan' );
	if($fields){
		$kompetensis = my_get_data_by_id('kompetensi','kompetensi_id',$fields['kompetensi_id']); 
		$uni_kompetensi = my_get_data_by_id('kompetensi_unit','kompetensi_unit_id',$kompetensis['kompetensi_unit_id']);
		$kel_kompetensi = my_get_data_by_id('kompetensi_kelompok','kompetensi_kelompok_id',$uni_kompetensi['kompetensi_kelompok_id']);
		$jenis = my_get_data_by_id( 'kompetensi_jenis' , 'kompetensi_jenis_id' , $kel_kompetensi['kompetensi_jenis_id']);
		$view .= form_field_display( $jenis['nama_kompetensi_jenis'] , 'Jenis kompetensi' );
	}else{
		$jenis = my_get_data_by_id( 'kompetensi_jenis' , 'kompetensi_jenis_id' , $jenis_id);
		$view .= form_field_display( $jenis['nama_kompetensi_jenis'] , 'Jenis kompetensi' );
	}
	
	$datas_kelompok_kompetensi = array();
	$query_kelompok_kompetensi = "SELECT * FROM kompetensi_kelompok WHERE kompetensi_jenis_id = {$jenis['kompetensi_jenis_id']}";
	$result_kelompok_kompetensi = my_query($query_kelompok_kompetensi);
	while($row_kelompok_kompetensi = my_fetch_array($result_kelompok_kompetensi)){
		$datas_kelompok_kompetensi[$row_kelompok_kompetensi['kompetensi_kelompok_id']] = $row_kelompok_kompetensi['nama_kompetensi_kelompok'];
	}
	
	$dirkom = array(
				'name'=>'kategori_kompetensi_id',
				'value'=>( isset($_POST['kategori_kompetensi_id']) ? $_POST['kategori_kompetensi_id'] :$uni_kompetensi['kompetensi_kelompok_id'] ),
				'id'=>'kategori_kompetensi_id',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_kelompok_kompetensi = form_dropdown($dirkom , $datas_kelompok_kompetensi);
 
	$unitkom = array( 
				'name'=>'kompetensi_unit_id',
				'value'=>( isset($_POST['kompetensi_unit_id']) ? $_POST['kompetensi_unit_id'] : ( $fields ? $uni_kompetensi['kompetensi_unit_id'] : 0 ) ),
				'id'=>'kompetensi_unit_id',
		);
	$dropdown_extend = dropdown_multi_rows_extends( $dirkom , $unitkom , $datas_kelompok_kompetensi , "combobox_unit_kompetensi.php"  );
	$view .= form_field_display( $dropdown_extend['parent']  , "Kelompok kompetensi"  ); 

	if($fields){
		$datas_unit_kompetensi = array();
		$query_unit_kompetensi = "SELECT * FROM kompetensi_unit WHERE kompetensi_kelompok_id = {$uni_kompetensi['kompetensi_kelompok_id']}";
		$result_unit_kompetensi = my_query($query_unit_kompetensi);
		while($row_unit_kompetensi = my_fetch_array($result_unit_kompetensi)){
			$datas_unit_kompetensi[$row_unit_kompetensi['kompetensi_unit_id']] = $row_unit_kompetensi['nama_kompetensi_unit'];
		}
		$kompetensi = array( 
				'name'=>'kompetensi_id',
				'value'=>( isset($_POST['kompetensi_id']) ? $_POST['kompetensi_id'] : $fields['kompetensi_id'] ),
				'id'=>'kompetensi_id',
		);
		$dropdown_extend = dropdown_multi_rows_extends( $unitkom , $kompetensi , $datas_unit_kompetensi , "combobox_kompetensi.php"  );
	
		$view .= form_field_display( $dropdown_extend['parent']  , 'Unit kompetensi' );
	}else{	
		$view .= form_field_display( $dropdown_extend['child']  , 'Unit kompetensi' );
	}
	
	$kompetensi = array( 
				'name'=>'kompetensi_id',
				'value'=>( isset($_POST['kompetensi_id']) ? $_POST['kompetensi_id'] : $fields['kompetensi_id'] ),
				'id'=>'kompetensi_id',
	);
	
	if($fields){
		$kompetesi_list = array();
		$query_kompetensi = "SELECT * FROM kompetensi  WHERE kompetensi_unit_id = {$uni_kompetensi['kompetensi_unit_id']}";
		$result_kompetensi = my_query($query_kompetensi);
		while($row_kompetensi = my_fetch_array($result_kompetensi)){
			$len = $row_kompetensi['nama_kompetensi'];
			if( strlen($len) >  65 ) 
			$kompetesi_list[$row_kompetensi['kompetensi_id']] = substr($row_kompetensi['nama_kompetensi'] ,0 ,  65)." ..."  ; 
			else
			$kompetesi_list[$row_kompetensi['kompetensi_id']] = $row_kompetensi['nama_kompetensi'];
		}
		$indikator = array( 
			'name'=>'level',
			'value'=>( isset($_POST['level']) ? $_POST['level'] : $fields['level'] ),
			'id'=>'level',
		);
		$dropdown_extend2 = dropdown_multi_rows_extends(   $kompetensi ,  $indikator ,$kompetesi_list, "combobox_kompetensi.php"  );
		$view .= form_field_display( $dropdown_extend2['parent']  , 'Kompetensi' );
	}else{	
		$dropdown_extend2 = dropdown_multi_rows_extends(   $unitkom ,  $kompetensi ,false, "combobox_kompetensi.php"  );
		$view .= form_field_display( $dropdown_extend2['child']  , 'Kompetensi' );
	}	

	$indikator = array( 
		'name'=>'level',
		'value'=>( isset($_POST['level']) ? $_POST['level'] : $fields['level'] ),
		'id'=>'level',
	);
	if($fields){
		$indikator_list = array();
		$query_indikator = "SELECT * FROM kompetensi_indikator  WHERE kompetensi_id = {$kompetensis['kompetensi_id']}";
		$result_indikator = my_query($query_indikator);
		while($row_indikator = my_fetch_array($result_indikator)){
			$len = $row_indikator['tingkat_id'].'-'.$row_indikator['indikator'];
			if( strlen($len) >  65 ) 
			$indikator_list[$row_indikator['tingkat_id']] = substr($row_indikator['tingkat_id'].'-'.$row_indikator['indikator'] ,0 ,  65)." ..."  ; 
			else
			$indikator_list[$row_indikator['tingkat_id']] = $row_indikator['tingkat_id'].'-'.$row_indikator['indikator'];
		}
		$dropdown_extend3 = dropdown_multi_rows_extends(   $indikator , false ,$indikator_list, "combobox_indikator.php"  );
		$view .= form_field_display( $dropdown_extend3['parent']  , 'Indikator' );
	}else{	
		$dropdown_extend3 = dropdown_multi_rows_extends(   $kompetensi ,  $indikator ,false, "combobox_indikator.php"  );
		$view .= form_field_display( $dropdown_extend3['child']  , 'Indikator' );
	}	

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_kompetensi_submit($jabatan_id, $id= 0){
	$datas = array(
		'jabatan_id'=>my_type_data_int($jabatan_id), 
		'kompetensi_id'=>my_type_data_int($_POST['kompetensi_id']), 		 	 	 	 	 	 	 
		'level'=>my_type_data_int($_POST['level']), 
	);

	if($id > 0){
		return my_update_record('jabatan_kompetensi_link','jabatan_kompetensi_link_id',$id, $datas);	
	}else{
		return my_insert_record('jabatan_kompetensi_link' , $datas);
	}	
}

function form_kompetensi_validate($jabatan_id,$id){
	$errsubmit = false;
	$err = array();
 
	$code =  (int) $_POST["kompetensi_id"]; 
	if( $code == 0){
		$errsubmit = true;
		$err[] = "Kompetensi belum di isi";
	}

	$query="SELECT jabatan_kompetensi_link_id FROM jabatan_kompetensi_link
		WHERE kompetensi_id=".$code.
		" AND jabatan_id=".$jabatan_id.
		" AND jabatan_kompetensi_link_id<>".$id ; 
	
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kompetensi sudah digunakan";	
	}
 
	$code =  (int) $_POST["level"]; 
	if( $code == 0){
		$errsubmit = true;
		$err[] = "Indikator belum di isi";
	} 
 
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_kompetensi($id){
	$query = "DELETE FROM jabatan_kompetensi_link WHERE jabatan_kompetensi_link_id ={$id} ";
	return my_query($query);
}

/*
===============================
	Physical Abilities
===============================
*/	

function list_physical($jabatan_id){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=physical_delete&id=\'+id;
			}
			return false;
		}		
	');	
 
	$header  = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Kualifikasi umum'=>array('style'=>'border-bottom:2px solid;width:55%'),  
		'Syarat minimum'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'),   
	);
	$query = "SELECT * FROM jabatan_physical_link a
		INNER JOIN jabatan_physical b ON a.physical_id = b.physical_id
		WHERE a.jabatan_id ={$jabatan_id}";
	$result = my_query($query);
	$i = 0;
	$row = array();
	while($ey = my_fetch_array($result) ){
		$i++;
		if ($ey['tipe_jawaban']=="Ya"){
			$edit_button = button_icon( 'b_edit_off.png' , '' );
		}else{
			$editproperty = array(
				'href'=>'index.php?com=jabatan_specification&task=physical_create&jabatan_id='.$jabatan_id.'&id='.$ey['jabatan_physical_link_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
				);	
			$edit_button = button_icon( 'b_edit.png' , $editproperty  );		
		}
		
		$deleteproperty = array(
			'href'=>'javascript:void();', 
			'title'=>'Delete',
			'onclick'=>'javascript:confirmDelete( '.$ey['jabatan_physical_link_id'].')'
			);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),    
			'nama'=>  $ey['nama_physical'] ,     
			'jawaban'=>  $ey['jawaban'] ,     
			'operasi'=> position_text_align( $edit_button .' '.$delete_button , 'right'),
		);
	}
	$datas = table_rows($row);

	$jabatan =loaddata_jabatan($jabatan_id) ;

	$view = form_header( "Physical Abilities" , "Physical Abilities"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=jabatan_specification&task=physical_create&jabatan_id='.$jabatan_id.'&id=0\' });" class="main_button" /> 
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=jabatan_specification\'"/>
	</div></td></tr>'   ; 
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nomor_posisi'] .'</span>'  , "<b>Nomor Posisi</b>"    );  	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nama_jabatan'].'</span>'   , "<b>Jabatan</b>"    );  
	$view .= form_field_display( '<span class="label_form">'.$jabatan['departemen'] .'</span>'  , "<b>Departemen</b>"    ); 
	return  $view. table_builder($header , $datas ,  4, false );
}

function form_physical_create($jabatan_id, $id=0){
	$fields = my_get_data_by_id('jabatan_physical_link','jabatan_physical_link_id',$id);
	$view = form_header( "Physical Ability" , "physical"  );

	if($id > 0){
		$physical_id=( isset($fields['physical_id']) ? $fields['physical_id'] : 0 ) ;
		$physical = my_get_data_by_id('jabatan_physical','physical_id',$physical_id);
		$view .= form_field_display( '<span class="label_form">'. $physical['nama_physical'] .'</span>', "<b>Physical Ability</b>"    );	
		$form_jawaban = form_display_jawaban($id,$physical_id) ;
		$view .= form_field_display( $form_jawaban   , "Syarat minimum");
	}else{
		$physicals = array();
		$query_physical = "SELECT * FROM jabatan_physical ORDER BY nama_physical";
		$re = my_query($query_physical);
		while($row = my_fetch_array($re) ){
			$physicals[$row['physical_id']] =$row['nama_physical'];
		}
		$physical = array(
			'name'=>'physical_id',
			'id'=>'physical_id',
			'value'=>isset($_POST['physical_id']) ? $_POST['physical_id'] : $fields['physical_id'] 
			);
		$form_physical = form_dropdown($physical , $physicals);
		$view .= form_field_display( $form_physical   , "Kualifikasi");
	}
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_physical_submit($jabatan_id, $id= 0){
	if($id > 0){
		$jawaban  = explode("|", $_POST['jawaban']);
		$datas = array(
			'jawaban'=>my_type_data_str($jawaban[1]), 		 	 	 	 	 	 	 
			'jawaban_skor'=>my_type_data_str($jawaban[0]), 		 	 	 	 	 	 	 
		);
		return my_update_record(
			'jabatan_physical_link',
			'jabatan_physical_link_id',
			$id, 
			$datas);	
	}
	$ph = my_get_data_by_id( 'jabatan_physical' , 'physical_id' , $_POST['physical_id']);
	if($ph['tipe_jawaban'] == 'Ya'){
		$datas = array(
			'physical_id'=>my_type_data_int($_POST['physical_id']), 		 	 	 	 	 	 	 
			'jabatan_id'=>my_type_data_int($jabatan_id), 
			'jawaban'=>my_type_data_str('Ya'), 		 	 	 	 	 	 	 
			'jawaban_skor'=>my_type_data_str(100), 		 	 	 	 	 	 	 
		); 
	}else{
		$datas = array(
			'physical_id'=>my_type_data_int($_POST['physical_id']), 		 	 	 	 	 	 	 
			'jabatan_id'=>my_type_data_int($jabatan_id), 
		);
	}
	return my_insert_record('jabatan_physical_link' , $datas);
}

function form_physical_validate($jabatan_id,$id){
	$errsubmit = false;
	$err = array();
 
	if($id==0){
		$code =  (int) $_POST["physical_id"]; 
		if( $code == 0){
			$errsubmit = true;
			$err[] = "Physical Ability belum diisi";
		}

		$query="SELECT jabatan_physical_link_id FROM jabatan_physical_link
			WHERE physical_id=".$code.
			" AND jabatan_id=".$jabatan_id.
			" AND jabatan_physical_link_id<>".$id ; 
		
		if (my_num_rows( my_query($query)) >0){
			$errsubmit = true;
			$err[] = "Physical Ability sudah digunakan";	
		}
	}
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_physical($id){
	$query = "DELETE FROM jabatan_physical_link WHERE jabatan_physical_link_id ={$id} ";
	return my_query($query);
}

function form_display_jawaban($id,$physical_id ){
	$physicals = my_get_data_by_id('jabatan_physical','physical_id',$physical_id);
	$jawabans = my_get_data_by_id('jabatan_physical_link','jabatan_physical_link_id',$id);
	$value = $jawabans['jawaban'];
	if($physicals['tipe_jawaban'] =='Opsi'){
		$opsis = explode("|" ,$physicals['catatan']);
		$opsi =array();
		foreach($opsis as $answer){
			$list_jawaban_skor = explode("," , $answer);
			 
			$pilihan_jawaban = trim($list_jawaban_skor[0]);
			$skor_jawaban = trim($list_jawaban_skor[1]);
			$skor = trim($skor_jawaban ,")");
			$pilihan = trim($pilihan_jawaban,"(");
			$opsi[$skor.'|'.$pilihan] =$pilihan;
		}
		$j = array(
			'name'=>'jawaban',
			'id'=>'jawaban',
			'value'=>$value,
		);
		$form_display = form_dropdown($j,$opsi);
	}elseif($physicals['tipe_jawaban'] =='Teks'){
		$j = array(
			'name'=>'jawaban',
			'id'=>'jawaban',
			'value'=>$value,
			'maxlength'=>( $physicals['catatan'] <>'' ? $physicals['catatan']: '20' ),
			'size'=>( $physicals['catatan'] <>'' ? $physicals['catatan']+2: '25' )
		);
		$form_display = form_dynamic($j );
	}

	return $form_display;
}	 